package com.xiaotu.spider.platformHome.pageprocessor;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.xiaotu.common.model.HomeRecommendModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.PageUtils;
import com.xiaotu.spider.AUrlPageProcessor;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.selector.Html;

/**
 * @类名 YouKuHomeRecommendPageProcessor
 * @日期 2020年3月11日
 * @作者 wyl
 * @功能 优酷网站首页推荐位
 */
@Component("YouKuHomeRecommendPageProcessor")
@Scope("prototype")
public class YouKuHomeRecommendPageProcessor extends AUrlPageProcessor{

	private static final String DIANSHIJU_URL = "https://tv.youku.com/?spm=a2hcb.12675304.m_6913.5~5~5~5~5~5~5~A";
	
	private static final String DIANYING_URL = "https://movie.youku.com/?spm=a2hcb.12675304.header-contain.5~5~5!2~5~1~3!2~A";
	
    //首页电影推荐,剧集页青春剧场、燃剧场，港剧场，电影页、网络电影页面获取不到数据
	@Override
    protected String[] getStartRequestUrls(){
    	return new String[]{"https://www.youku.com/"};
    }

    @Override
    public String getPageDataType(Page page)
    {
        String url = page.getUrl().toString();
        if (url.contains("tv.youku.com/?spm")){
        	return Constants.PageDataType.TV_BASICINFO;
        }else if(url.contains("movie.youku.com/?spm")){
        	return Constants.PageDataType.TV_RECORD;
        }else{
        	return Constants.PageDataType.SEARCH_LIST;
        }
    }

    @Override
    public void process(Page page){
        String dataType = this.getPageDataType(page);
        switch (dataType)
        {
            case Constants.PageDataType.SEARCH_LIST:
                this.searchListProcess(page);
                break;
            case Constants.PageDataType.TV_BASICINFO:
                this.tvRecordProcess(page);
                break;
            case Constants.PageDataType.TV_RECORD:
                this.mvRecordProcess(page);
                break;
            default:
                break;
        }
    }
    
    //优酷首页推荐
    private void searchListProcess(Page page){
    	List<HomeRecommendModel> homeCarouselList = new ArrayList<HomeRecommendModel>();
        //首页推荐位轮播
        List<String> homeCarouselHTML = page.getHtml()
                .xpath("//div[@class='swiper-wrapper']/div").all();
        for (int i = 0; i < homeCarouselHTML.size(); i++) {
        	Html homeCarouselHtml = new Html(homeCarouselHTML.get(i));
			String name = homeCarouselHtml.xpath("//div/a/@title").get();
			homeCarouselList.add(new HomeRecommendModel(i+1, name, "优酷", "首页轮播"));
		}
        
        //首页剧集推荐位
        List<String> homeTVHTML = page.getHtml()
                .xpath("//div[@class='ant-tabs-tabpane ant-tabs-tabpane-active']/div[@class='g-row packfocuslist_tworowcontent']"
                		+ "/div[@class='trochanter_trochanterroot trochanter_focuscontent']//div"
                		+ "/div[@class='trochanter_trochantercontent']/div[@class='g-row undefined']/div").all();
        for (int i = 0; i < homeTVHTML.size(); i++) {
        	Html homeTVHTMLHtml = new Html(homeTVHTML.get(i));
			String name = homeTVHTMLHtml.xpath("//div[@class='pack_pack_cover']/a/@title").get();
			if(name.contains("🔥")){
				name = name.substring(0,name.indexOf("🔥"));
			}else if(name.contains("·")){
				name = name.substring(0,name.indexOf("·"));
			}else if(name.contains("❤")){
				name = name.substring(0,name.indexOf("❤"));
			}
			homeCarouselList.add(new HomeRecommendModel(i+1, name, "优酷", "首页剧集"));
		}
        
        page.putField(HomeRecommendModel.class.getName(), homeCarouselList);
        PageUtils.addTargetRequest(page, DIANSHIJU_URL, page.getRequest().getExtras());
    }

    //优酷电视剧推荐
    private void tvRecordProcess(Page page){
    	List<HomeRecommendModel> homeTVList = new ArrayList<HomeRecommendModel>();
        //电视剧轮播
        List<String> homeTVHTML = page.getHtml()
                .xpath("//div[@class='swiper-wrapper']/div").all();
        for (int i = 0; i < homeTVHTML.size(); i++) {
        	Html homeTVHtml = new Html(homeTVHTML.get(i));
			String name = homeTVHtml.xpath("//a/@title").get();
			homeTVList.add(new HomeRecommendModel(i+1, name, "优酷", "电视剧轮播"));
		}
        
        //电视剧最新热剧
        List<String> homeNewHotTVHTML = page.getHtml()
                .xpath("//div[@id='m_5295']/div/div[@data-spm='m_5295_c_12649']/div/div[@class='trochanter_trochanterroot ']"
                		+ "/div[@class='trochanter_trochantercomponent ']/div[@class='trochanter_trochantercontent']/div[@class='g-row']/div").all();
        for (int i = 0; i < homeNewHotTVHTML.size(); i++) {
        	Html homeNewHotTVHtml = new Html(homeNewHotTVHTML.get(i));
			String name = homeNewHotTVHtml.xpath("//div[@class='pack_pack_cover']/a/@title").get();
			homeTVList.add(new HomeRecommendModel(i+1, name, "优酷", "电视剧最新热剧"));
		}
        
        //电视剧热度榜
        getTVLeaderboard(page, homeTVList, "1", "电视剧热度榜");
        //电视剧高分榜
        getTVLeaderboard(page, homeTVList, "2", "电视剧高分榜");
        //电视剧独播榜
        getTVLeaderboard(page, homeTVList, "3", "电视剧独播榜");
        //电视剧港剧榜
        getTVLeaderboard(page, homeTVList, "4", "电视剧港剧榜");
        //电视剧悬疑榜
        getTVLeaderboard(page, homeTVList, "5", "电视剧悬疑榜");
        //电视剧高清榜
        getTVLeaderboard(page, homeTVList, "6", "电视剧高清榜");
        //电视剧古装榜
        getTVLeaderboard(page, homeTVList, "7", "电视剧古装榜");
        //电视剧开心榜
        getTVLeaderboard(page, homeTVList, "8", "电视剧开心榜");
        //电视剧热血榜
        getTVLeaderboard(page, homeTVList, "9", "电视剧热血榜");
        //电视剧海外榜
        getTVLeaderboard(page, homeTVList, "10", "电视剧海外榜");
        
        page.putField(HomeRecommendModel.class.getName(), homeTVList);
        PageUtils.addTargetRequest(page, DIANYING_URL, page.getRequest().getExtras());
    }
    
    //优酷电影推荐
    private void mvRecordProcess(Page page){
    	List<HomeRecommendModel> homeMovieList = new ArrayList<HomeRecommendModel>();
    	
    	//电影轮播
    	List<String> homeMovieHTML = page.getHtml()
                .xpath("//div[@class='swiper-wrapper']/div").all();
    	for (int i = 0; i < homeMovieHTML.size(); i++) {
    		Html homeMovieHtml = new Html(homeMovieHTML.get(i));
			String name = homeMovieHtml.xpath("//div/a/@title").get();
			homeMovieList.add(new HomeRecommendModel(i+1, name, "优酷", "电影轮播"));
		}
    	//电影预告
    	List<String> homeMoviePrevueHTML = page.getHtml()
                .xpath("//div[@id='m_4422']/div/div[@data-spm='m_4422_c_11052']/div/"
                		+ "div[@class='trochanter_trochanterroot trochanter_focuscontent']"
                		+ "/div/div[@class='trochanter_trochantercontent']/div[@class='g-row undefined']/div").all();
    	for (int i = 0; i < homeMoviePrevueHTML.size(); i++) {
    		Html homeMoviePrevueHtml = new Html(homeMoviePrevueHTML.get(i));
			String name = homeMoviePrevueHtml.xpath("//div[@class='pack_pack_cover']/a/@title").get();
			homeMovieList.add(new HomeRecommendModel(i+1, name, "优酷", "电影预告"));
		}
    	
    	//电影热播榜
        getMovieLeaderboard(page, homeMovieList, "1", "电影热播榜");
        //电影新片榜
        getMovieLeaderboard(page, homeMovieList, "2", "电影新片榜");
        //电影高分榜
        getMovieLeaderboard(page, homeMovieList, "3", "电影高分榜");
        //电影免费榜
        getMovieLeaderboard(page, homeMovieList, "4", "电影免费榜");
        //电影网大榜
        getMovieLeaderboard(page, homeMovieList, "5", "电影网大榜");
        //电影预告榜
        getMovieLeaderboard(page, homeMovieList, "6", "电影预告榜");
        //电影喜剧榜
        getMovieLeaderboard(page, homeMovieList, "7", "电影喜剧榜");
        //电影动作榜
        getMovieLeaderboard(page, homeMovieList, "8", "电影动作榜");
    	
        //电影正在热播
        List<String> homeMovieBeHotHTML = page.getHtml()
                .xpath("//div[@id='m_4423']/div/div[@data-spm='m_4423_c_11053']/div/"
                		+ "div[@class='trochanter_trochanterroot ']"
                		+ "/div[@class='trochanter_trochantercomponent ']/div/div[@class='g-row']/div").all();
    	for (int i = 0; i < homeMovieBeHotHTML.size(); i++) {
    		Html homeMovieBeHotHtml = new Html(homeMovieBeHotHTML.get(i));
			String name = homeMovieBeHotHtml.xpath("//div[@class='pack_pack_cover']/a/@title").get();
			homeMovieList.add(new HomeRecommendModel(i+1, name, "优酷", "电影正在热播"));
		}
    }
    
    //电视剧热门榜单公用方法
    public void getTVLeaderboard(Page page,List<HomeRecommendModel> homeTVList,String divType,String siteType){
    	List<String> homeTVHTML = page.getHtml()
            .xpath("//div[@id='m_5297']/div[@class='common_container module_mod_bottom']/div"
        		+ "/div[@class='ant-tabs-content ant-tabs-content-no-animated ant-tabs-top-content']"
        		+ "/div[@data-spm='m_5297_c_12209_"+divType+"']/div/div[@class='trochanter_trochanterroot ']"
        		+ "/div[@class='trochanter_trochantercomponent ']/div[@class='trochanter_trochantercontent']/div[@class='g-row']/div").all();
        for (int i = 0; i < homeTVHTML.size(); i++) {
        	Html homeTVHtml = new Html(homeTVHTML.get(i));
			String name = homeTVHtml.xpath("//div[@class='pack_pack_cover']/a/@title").get();
			homeTVList.add(new HomeRecommendModel(i+1, name, "优酷", siteType));
		}
    }
    
    //电影排行榜单公用方法
    public void getMovieLeaderboard(Page page,List<HomeRecommendModel> homeMovieList,String divType,String siteType){
    	List<String> homeMovieHTML = page.getHtml()
            .xpath("//div[@id='m_4424']/div[@class='common_container module_mod_bottom']/div"
        		+ "/div[@class='ant-tabs-content ant-tabs-content-no-animated ant-tabs-top-content']"
        		+ "/div[@data-spm='m_4424_c_11054_"+divType+"']/div/div[@class='trochanter_trochanterroot ']"
        		+ "/div[@class='trochanter_trochantercomponent ']/div[@class='trochanter_trochantercontent']/div[@class='g-row']/div").all();
        for (int i = 0; i < homeMovieHTML.size(); i++) {
        	Html homeMovieHtml = new Html(homeMovieHTML.get(i));
			String name = homeMovieHtml.xpath("//div[@class='pack_pack_cover']/a/@title").get();
			homeMovieList.add(new HomeRecommendModel(i+1, name, "优酷", siteType));
		}
    }

    @Override
    protected String getSpiderType()
    {
        return Constants.SpiderTaskType.YOUKU_HOME_RECOMMEND;
    }
    
}
